/**
 * AS3 Tree
 *
 * @author		Duarte Peixinho
 * @version		1.0
 */

package com.duartepeixinho.tree 
{
    
    import flash.display.Sprite;

	public class Branches extends Sprite
	{
		
		public var childsOver : Boolean = false;
		private var angle:Number;
		private var extension:Number;
		private var thickness:int;
		private var thicknessInit:int;

		private var angleInterval:Number;
		private var lineSize:Number;
		private var posX:Number = 0;
		private var posY:Number = 0;
		private var extensionComplete:Number = 0;
		
		//branches
		private var branches:Array = new Array();
		private var branchesNumber:Number = 0;
		private var actualBranches:Number = 0;
		private var i:int = 1;
		
		//lineSize calculation
		public var lineDone:Boolean = true;
		private var angleCalc:Number;
		private var posXinit:Number = 0;
		private var posYinit:Number = 0;
		
		/**
		 * Branches
		 * @param	extension			Number		Extension of the branch
		 * @param	thickness			int			Thickness of the branch
		 * @param	branchesNumber		Number		How many branches would have this branche
		 * @param	lineSize			Number		LineSize
		 * @param	angle				Number		Angle that the branch will grow
		 * @param	angleInterval		Number		Angle interval that the branch will grow
		 */
		public function Branches(extension:Number, thickness:int, branchesNumber:Number = 5, lineSize:Number = 5, angle:Number = 90, angleInterval:Number = 30 ) 
		{
			
			this.angle = angle;
			this.extension = extension;
			this.thickness = thickness;
			this.thicknessInit = thickness;
			this.angleInterval = angleInterval;
			this.lineSize = lineSize;
			this.branchesNumber = branchesNumber;
		}
		
		/**
		 * loop
		 */
		public function processFrame():void {
			//draw here
			if (extension>extensionComplete) {
				
				if (lineDone) {
					posXinit = posX;
					posYinit = posY;
					angleCalc = Math.random() * ((angle + angleInterval) - (angle-angleInterval)) + (angle-angleInterval);
					lineDone = false;
					
				}				
				graphics.lineStyle(thickness);
				posX += Math.cos(angleCalc * Math.PI / 180) * lineSize;			
				posY += Math.sin(angleCalc * Math.PI / 180) * lineSize;	
				extensionComplete += Math.round(Math.sqrt(Math.pow(posX - posXinit, 2) + Math.pow(posY - posYinit, 2)));
				thickness = thicknessInit - (thicknessInit * extensionComplete / extension);
				graphics.lineTo(posX, posY);
				
				if (Math.sqrt(Math.pow(lineSize, 2)) < Math.sqrt(Math.pow(posX - posXinit, 2) + Math.pow(posY - posYinit, 2))) {
					lineDone = true;
				}
			
						
			//branches
			if (actualBranches < branchesNumber) {
				//tweak numbers here to create the branches
				if (extensionComplete >= extension / branchesNumber * i) {
					branches[i - 1] = new Branches(extension/2, thickness, Math.random()*10+5, lineSize/2, Math.random()*(angle-90), 30);
					branches[i - 1].x = posX;
					branches[i - 1].y = posY;
					addChild(branches[i - 1]);
					actualBranches++;
					i++;
				}
			}
				if (!childsOver) {
					var l:int=0;
					for (var k:int = 0; k < branches.length; k++) {
						branches[k].processFrame();
						if (branches[k].lineDone) l++;
					}			
					if (k==branchesNumber) this.childsOver=true;
				}
			}
		}
	}
}
